﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Truextend.Training.Business.Dtos;
using Microsoft.SharePoint;
using Truextend.Training.Model.Entities;
using Truextend.Training.Business;
using Truextend.Training.Business.Interfaces;

namespace Truextend.Training.Service
{
    public class CourseInstanceService : BaseService
    {

        public CourseInstance AddCourseInstance(CourseInstance courseInstance)
        {
            ICourseInstanceManager courseInstanceManager = null;
            ExecuteManager(uow =>
            {
                courseInstanceManager = new CourseInstanceManager(uow);                
                courseInstanceManager.AddCourseInstance(courseInstance);
            } );
            return courseInstance;
        }

        public CourseInstance EditCourseInstance(CourseInstance courseInstance)
        {
            ICourseInstanceManager courseInstanceManager = null;
            ExecuteManager(uow =>
            {
                courseInstanceManager = new CourseInstanceManager(uow);
                courseInstanceManager.EditCourseInstance(courseInstance);
            });
            return courseInstance;
        }

        public IList<CourseInstanceInstructorDto> GetAllCourseInstancesAvailables()
        {
            IList<CourseInstance> list = null;
            ICourseInstanceManager courseInstanceManager = null;
            ExecuteManager(uow =>
            {
                courseInstanceManager = new CourseInstanceManager(uow);
                list = courseInstanceManager.GetAllCourseInstancesAvailable();
            });

            SPSite site = SPContext.Current.Site;
            SPWeb web = site.OpenWeb();
            IList<CourseInstanceInstructorDto> ciDtoList = new List<CourseInstanceInstructorDto>();
            foreach(CourseInstance ci in list)
            {
                CourseInstanceInstructorDto ciDto = new CourseInstanceInstructorDto
                                                        {
                                                            CourseInstanceId = ci.CourseInstanceId,
                                                            Location = ci.Location,
                                                            StartDate = ci.StartDate,
                                                            EndDate = ci.EndDate,
                                                            CourseName = ci.Course.Name
                                                        };

                SPUser instructor = web.AllUsers.GetByID(ci.InstructorId);
                ciDto.InstructorName = instructor.Name;

                ciDtoList.Add(ciDto);
            }
            
            return ciDtoList.ToList();
        }


        public IList<CourseInstanceRegisterDto> GetAllCourseInstancesRegisterAvailables()
        {
            IList<CourseInstance> list = null;
            ICourseInstanceManager courseInstanceManager = null;
            ExecuteManager(uow =>
            {
                courseInstanceManager = new CourseInstanceManager(uow);
                list = courseInstanceManager.GetAllCourseInstancesAvailable();
            });

            SPSite site = SPContext.Current.Site;
            SPWeb web = site.OpenWeb();
            IList<CourseInstanceRegisterDto> ciDtoList = new List<CourseInstanceRegisterDto>();
            foreach (CourseInstance ci in list)
            {
                CourseInstanceRegisterDto ciDto = new CourseInstanceRegisterDto
                {
                    CourseInstanceId = ci.CourseInstanceId,
                    Location = ci.Location,
                    StartDate = ci.StartDate,
                    EndDate = ci.EndDate,
                    CourseName = ci.Course.Name,
                    InstructorId = ci.InstructorId,
                    CourseCategory = ci.Course.Category,
                    CourseDirectedTo = ci.Course.DirectedTo,
                    TotalParticipants = 10,
                    CurrentParticipants = 3
                };

                SPUser instructor = web.AllUsers.GetByID(ci.InstructorId);
                ciDto.InstructorName = instructor.Name;

                ciDtoList.Add(ciDto);
            }

            return ciDtoList.ToList();
        }

        public CourseInstance GetCourseInstanceById(long courseInstanceId)
        {
            CourseInstance courseInstance = null;
            CourseInstanceManager courseInstanceManager = null; 

            ExecuteManager(uow =>
            {
                courseInstanceManager = new CourseInstanceManager(uow);
                courseInstance = courseInstanceManager.GetCourseInstanceById(courseInstanceId);
            });

            SPSite site = SPContext.Current.Site;
            SPWeb web = site.OpenWeb();

            SPUser instructor = web.AllUsers.GetByID(courseInstance.InstructorId);
            courseInstance.InstructorName = instructor.Name;

            return courseInstance;
        }

        public IList<CourseInstance> GetAllCourseInstances()
        {
            IList<CourseInstance> list = null;
            ICourseInstanceManager courseInstanceManager = null;
            ExecuteManager(uow =>
            {
                courseInstanceManager = new CourseInstanceManager(uow);
                list = courseInstanceManager.GetAllCourseInstancesAvailable();
            });
                       
            return list.ToList();
        }
    }
}
